<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use Illuminate\View\Compilers\BladeCompiler;

class PrintTempletController extends Controller
{
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.templet.list', compact('menu_id', 'toolBarButton'));
    }

    public function listData()
    {
        try{
            $list = callApiServer("System.PrintTempletService.getInfoList");
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
        return view('system.templet.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('templet_id');
        try{
            $info = callApiServer("System.PrintTempletService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.templet.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['code'] = request('code');
        $params['description'] = request('description');
        $params['templet'] = request('templet');
        $params['data_sql'] = request('data_sql');
        $params['is_active'] = request('is_active');

        $rs = callApiServer("System.PrintTempletService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function selectTemplet()
    {
        $params['record_id'] = request('record_id');
        $params['code'] = request('templet_code');
        $info = callApiServer("System.PrintTempletService.getTempletByCode", $params);
        return view('system.templet.templet', compact("info", 'params'));
    }

    public function templetPrint()
    {
        $params['record_id'] = request('record_id');
        $params['templet_id'] = request('templet_id');
        $info = callApiServer("System.PrintTempletService.templetPrint", $params);
        //$blade = new BladeCompiler();
        //dd($blade->compileString($info['templet']));
        return view (['template' => $info['templet']], ['info' => $info]);
        //return view('system.templet.print', compact("info"));
    }
}
